package cn.iocoder.yudao.module.custom.controller.admin.merchantkey;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.custom.controller.admin.merchantkey.vo.*;
import cn.iocoder.yudao.module.custom.dal.dataobject.merchantkey.MerchantKeyDO;
import cn.iocoder.yudao.module.custom.convert.merchantkey.MerchantKeyConvert;
import cn.iocoder.yudao.module.custom.service.merchantkey.MerchantKeyService;

@Tag(name = "管理后台 - 商户私钥")
@RestController
@RequestMapping("/custom/merchant-key")
@Validated
public class MerchantKeyController {

    @Resource
    private MerchantKeyService merchantKeyService;

    @PostMapping("/create")
    @Operation(summary = "创建商户私钥")
    @PreAuthorize("@ss.hasPermission('custom:merchant-key:create')")
    public CommonResult<Integer> createMerchantKey(@Valid @RequestBody MerchantKeyCreateReqVO createReqVO) {
        return success(merchantKeyService.createMerchantKey(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商户私钥")
    @PreAuthorize("@ss.hasPermission('custom:merchant-key:update')")
    public CommonResult<Boolean> updateMerchantKey(@Valid @RequestBody MerchantKeyUpdateReqVO updateReqVO) {
        merchantKeyService.updateMerchantKey(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商户私钥")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('custom:merchant-key:delete')")
    public CommonResult<Boolean> deleteMerchantKey(@RequestParam("id") Integer id) {
        merchantKeyService.deleteMerchantKey(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商户私钥")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('custom:merchant-key:query')")
    public CommonResult<MerchantKeyRespVO> getMerchantKey(@RequestParam("id") Integer id) {
        MerchantKeyDO merchantKey = merchantKeyService.getMerchantKey(id);
        return success(MerchantKeyConvert.INSTANCE.convert(merchantKey));
    }

    @GetMapping("/list")
    @Operation(summary = "获得商户私钥列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('custom:merchant-key:query')")
    public CommonResult<List<MerchantKeyRespVO>> getMerchantKeyList(@RequestParam("ids") Collection<Integer> ids) {
        List<MerchantKeyDO> list = merchantKeyService.getMerchantKeyList(ids);
        return success(MerchantKeyConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商户私钥分页")
    @PreAuthorize("@ss.hasPermission('custom:merchant-key:query')")
    public CommonResult<PageResult<MerchantKeyRespVO>> getMerchantKeyPage(@Valid MerchantKeyPageReqVO pageVO) {
        PageResult<MerchantKeyDO> pageResult = merchantKeyService.getMerchantKeyPage(pageVO);
        return success(MerchantKeyConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商户私钥 Excel")
    @PreAuthorize("@ss.hasPermission('custom:merchant-key:export')")
    @OperateLog(type = EXPORT)
    public void exportMerchantKeyExcel(@Valid MerchantKeyExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MerchantKeyDO> list = merchantKeyService.getMerchantKeyList(exportReqVO);
        // 导出 Excel
        List<MerchantKeyExcelVO> datas = MerchantKeyConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "商户私钥.xls", "数据", MerchantKeyExcelVO.class, datas);
    }

}
